<?php
session_start();
date_default_timezone_set('Asia/Singapore');
include('class/Photo.class.php');
include('class/User.class.php');
$output_dir = "images/profile/";
 
if(isset($_FILES["uploaded_file"]))
{
    //Filter the file types , if you want.
    if ($_FILES["uploaded_file"]["error"] > 0)
    {
      echo "Error: " . $_FILES["file"]["error"] . "<br>";
    }
    else
    {
		$allowed_files = array('image/jpg', 'image/gif', 'image/png', 'image/jpeg');
        //move the uploaded file to uploads folder;
		$part2 = rand();
		$datetime = new DateTime;
		$timezone  = new DateTimeZone('Singapore');
		$datetime->setTimezone($timezone);
		//$date_time = $datetime->format('Y-m-d H:i:s');
		$date_time = $datetime->format('YmdHis');
		$part1 = $_SESSION['auth'];
		$name = $date_time . $part1 . $part2;
		
		preg_match('/.*\.(.*?)$/si', $_FILES["uploaded_file"]["name"], $match);
		if (isset($match[1])) {
			for($i = 0; $i <= 3; $i++) {
				if($allowed_files[$i] == $_FILES["uploaded_file"]["type"]) {
					$new_filename = $name . "." . $match[1];
					move_uploaded_file($_FILES["uploaded_file"]["tmp_name"],$output_dir. $new_filename);
					$photos = new Photo();
					$photos->add_profile_photo($new_filename);
				}
			}
		}
		else {
			$new_filename = $name;
			//delete that file
		}
    }
}

$user = new User();
$file = $user->get_photo($_SESSION['auth']);
echo $file;
?>